package com.example.ParseXML;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;

public class ParseContainer {

	DefineContainer container;

	Document dom;

	public ParseContainer(String strPath) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			FileInputStream fos = new FileInputStream(strPath);
			// Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			// parse using builder to get DOM representation of the XML file
			dom = db.parse(fos);

		} catch (ParserConfigurationException pce) {
			Log.v("PCE: ", pce.getMessage());
		} catch (SAXException se) {
			Log.v("SE: " , se.getMessage());
		} catch (IOException ioe) {
			Log.v("IOE: " , ioe.getMessage());
		}
	}

	public DefineContainer getContainer() {
		container = new DefineContainer();
		Element docEle = dom.getDocumentElement();
		container.setVersion(docEle.getAttribute("version"));
		container.setXmlns(docEle.getAttribute("xmlns"));

		NodeList listRootfiles = docEle.getElementsByTagName("rootfiles");
		Element rootfiles = (Element) listRootfiles.item(0);
		NodeList listRootfile =  rootfiles.getElementsByTagName("rootfile");
		Element rootfile = (Element) listRootfile.item(0);
		container.setFull_path(rootfile.getAttribute("full-path"));
		container.setMedia_type(rootfile.getAttribute("media-type"));

		return container;
	}

}
